package com.example.esp8266.Equipment.Equipment_Fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.esp8266.Equipment.Equipment_CtrlActivity;
import com.example.esp8266.Equipment.Fragment_CallBack;
import com.example.esp8266.Equipment.Task_Fragment.TaskRun_Fragment;
import com.example.esp8266.Main.MainActivity;
import com.example.esp8266.R;

public class MainControl_Fragment extends Fragment
        implements View.OnClickListener {

    private View root;
    private Fragment_CallBack callback;
    private String target_device;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 获取传递的数据
        Bundle args = getArguments();
        if (args != null) {
            target_device = args.getString("target_device", "");
        }
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        if (context instanceof Fragment_CallBack) {
            callback = (Fragment_CallBack) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement Fragment_CallBack");
        }
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        root = inflater.inflate(R.layout.home_control_fragment, container, false);

        View btn1 = root.findViewById(R.id.btn1);
        btn1.setOnClickListener(this);//继电器按钮
        View btn2 = root.findViewById(R.id.btn2);
        btn2.setOnClickListener(this);//电风扇按钮
        View btn3 = root.findViewById(R.id.btn3);
        btn3.setOnClickListener(this);//传感器按钮
        //View btn4 = root.findViewById(R.id.btn4);
        //btn4.setOnClickListener(this);//报警信息按钮
        View btn7 = root.findViewById(R.id.btn7);
        btn7.setOnClickListener(this);//逻辑控制按钮

        View btn8 = root.findViewById(R.id.btn8);
        btn8.setOnClickListener(this);//退出按钮

        return root;
    }

    @Override//调用OnClickListener接口处理各种单击事件
    public void onClick(View v) {
        if (v.getId() == R.id.btn1) {//切换到继电器控制界面
            Bundle args = new Bundle();
            args.putString("back_info", "MainControlFragment");
            args.putString("target_device", target_device);
            Relay_Fragment relayFragment = new Relay_Fragment();
            // 将 args 对象设置为 Fragment 的参数
            relayFragment.setArguments(args);
            replaceFragment(relayFragment);
        } else if (v.getId() == R.id.btn8) {//退出设置控制界面
            // 创建一个新的Intent
            Intent intent = new Intent(getActivity(), MainActivity.class);
            // 清除之前的所有Activity堆栈，确保登录界面是用户会见到的第一个界面
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            // 启动SigninActivity
            startActivity(intent);
        } else if (v.getId() == R.id.btn2) {//切换到电风扇控制界面
            Bundle args = new Bundle();
            args.putString("back_info", "MainControlFragment");
            args.putString("target_device", target_device);
            Fan_Fragment fanFragment = new Fan_Fragment();
            // 将 args 对象设置为 Fragment 的参数
            fanFragment.setArguments(args);
            replaceFragment(fanFragment);
        } else if (v.getId() == R.id.btn3) {//切换到传感器界面
            Equipment_CtrlActivity activity = (Equipment_CtrlActivity) getActivity();
            if (activity != null) {
                // 调用Activity的方法来切换视图
                activity.switch_view(2);
            }
            Bundle args = new Bundle();
            args.putString("back_info", "MainControlFragment");
            args.putString("target_device", target_device);
            SensorALL_Fragment sensor_allFragment = new SensorALL_Fragment();
            // 将 args 对象设置为 Fragment 的参数
            sensor_allFragment.setArguments(args);
            replaceFragment(sensor_allFragment);
        } else if (v.getId() == R.id.btn7) {//切换到逻辑控制界面
            Equipment_CtrlActivity activity = (Equipment_CtrlActivity) getActivity();
            if (activity != null) {
                // 调用Activity的方法来切换视图
                activity.switch_view(2);
            }
            Bundle args = new Bundle();
            args.putString("back_info", "MainControlFragment");
            args.putString("target_device", target_device);
            TaskRun_Fragment autoRunFragment = new TaskRun_Fragment();
            // 将 args 对象设置为 Fragment 的参数
            autoRunFragment.setArguments(args);
            replaceFragment(autoRunFragment);
        } else if (v.getId() == R.id.btn4) {//切换到报警信息界面
            Equipment_CtrlActivity activity = (Equipment_CtrlActivity) getActivity();
            if (activity != null) {
                // 调用Activity的方法来切换视图
                activity.switch_view(2);
            }
            Bundle args = new Bundle();
            args.putString("back_info", "MainControlFragment");
            args.putString("target_device", target_device);
            Warning_Fragment warningFragment = new Warning_Fragment();
            // 将 args 对象设置为 Fragment 的参数
            warningFragment.setArguments(args);
            replaceFragment(warningFragment);

        }
    }

    //动态切换视图
    private void replaceFragment(Fragment fragment) {
        if (getActivity() != null) {
            FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            transaction.setCustomAnimations(R.anim.slide_left, R.anim.slide_right);
            transaction.replace(R.id.control_fragment_view, fragment);
            transaction.commit();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        // 在 Fragment 生命周期结束时执行的操作
        callback = null;
    }

    // 调用 callback 方法
    // 向Activity发数据
    private void sendMessageToActivity(String msg) {
        if (callback != null) {
            callback.sendMsgToActivity(msg);
        }
    }

    // 向Activity取数据
    private void receiveMessageFromActivity() {
        if (callback != null) {
            String message = callback.getMsgFromActivity("Request Message");
            Log.d("YourFragmentClass", "Message from Activity: " + message);
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        callback = null;
    }

}